package java111;

// https://www.nowcoder.com/practice/f78a359491e64a50bce2d89cff857eb6?tpId=13&tqId=11199&ru=/exam/oj
public class Day5 {
    public static void main(String[] args) {
        System.out.println(LastRemaining_Solution(88, 10));
    }
    public static int LastRemaining_Solution (int n, int m) {
        // write code here
        int[] arr = new int[n];
        boolean[] flag = new boolean[n];
        int count = n;
        int i = 0, tag;
        while(count > 1){
            tag = 0;  // 每次数m-1个有效数字
            for(; tag < m; i++){
                i %= n;
                if(!flag[i]){
                    tag++;
                }
            }
            flag[i-1] = true;
            count--;
            if(count == 1){
                break;
            }
            i %= n;
            while(flag[i]){
                i++;
                i %= n;
            }
        }
        for(int j = 0; j < flag.length; j++){
            if(!flag[j]){
                return j;
            }
        }
        return 0;
    }

}
